package com.share.example.selenium.web;

import com.share.common.common.Result;
import com.share.example.selenium.enums.BusinessEnum;
import com.share.example.selenium.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private CommonService commonService;

    //获取code
    private String getProcessCode(String processCode) {
        BusinessEnum businessEnum = BusinessEnum.toEnum(processCode);
        if (businessEnum == null) {
            processCode = BusinessEnum.DEFAULT.getCode();
        }
        log.info("processCode={}", processCode);
        return processCode;
    }

    //重新注册
    @RequestMapping("/reInitDriver")
    public Result openUrl(String processCode) {
        processCode = getProcessCode(processCode);
        return Result.bool(commonService.reInitDriver(processCode));
    }

    //打开首页
    @RequestMapping("/openUrl")
    public Result openUrl(String url, Integer delaySecord, String processCode) {
        processCode = getProcessCode(processCode);
        if (delaySecord == null) {
            delaySecord = 2;
        }
        return Result.bool(commonService.openUrl(url, delaySecord, processCode));
    }

    //刷新程序
    @RequestMapping("/refreshChrome")
    public Result refreshChrome(Integer delaySecord, String processCode) {
        processCode = getProcessCode(processCode);
        if (delaySecord == null) {
            delaySecord = 2;
        }
        return Result.bool(commonService.refreshChrome(delaySecord, processCode));
    }

    //列出所有的tab
    @RequestMapping("/listWindows")
    public Result listWindows(String processCode) {
        processCode = getProcessCode(processCode);
        return Result.success(commonService.listWindows(processCode));
    }

    //切换tab
    @RequestMapping("/switchToTarget")
    public Result switchToTarget(String desTarget, String processCode) {
        processCode = getProcessCode(processCode);
        if (StringUtils.isBlank(desTarget)) {
            return Result.failure("desTarget 参数不能为空");
        }
        return Result.success(commonService.switchToTarget(desTarget, processCode));
    }

    //关闭tab页
    @RequestMapping("/closeTab")
    public Result closeTab(String desTarget, String processCode) {
        processCode = getProcessCode(processCode);
        if (StringUtils.isBlank(desTarget)) {
            return Result.failure("desTarget 参数不能为空");
        }
        return Result.success(commonService.closeTab(desTarget, processCode));
    }

    //执行goovy脚本---通过code
    @RequestMapping("/exeGroovyByCode")
    public Result exeGroovyByCode(String code, String processCode) {
        processCode = getProcessCode(processCode);

        if (StringUtils.isBlank(code)) {
            return Result.failure("code 参数不能为空");
        }
        try {
            return Result.success(commonService.exeGroovyByCode(code, processCode));
        } catch (Exception e) {
            return Result.failure(e.getMessage());
        }

    }

    //执行goovy脚本---通过id
    @RequestMapping("/exeGroovyById")
    public Result exeGroovyById(String id, String processCode) {
        processCode = getProcessCode(processCode);

        if (StringUtils.isBlank(id)) {
            return Result.failure("id 参数不能为空");
        }
        try {
            return Result.success(commonService.exeGroovyById(id, processCode));
        } catch (Exception e) {
            return Result.failure(e.getMessage());
        }
    }

    //获取cookie信息
    @RequestMapping("/getCookies")
    public Result getCookies(String processCode) {
        processCode = getProcessCode(processCode);
        return Result.success(commonService.getCookies(processCode));
    }

    //设置 cookie信息
    @RequestMapping("/setOneCookie")
    public Result setCookie(String processCode, String cookieStr) {
        processCode = getProcessCode(processCode);
        return Result.insertOrDeleteOrUpdate(commonService.setOneCookie(processCode, cookieStr));
    }

    //设置 cookie信息
    @RequestMapping("/loadCookies")
    public Result loadCookies(String processCode, String cookieSetStr) {
        processCode = getProcessCode(processCode);
        return Result.insertOrDeleteOrUpdate(commonService.loadCookies(processCode, cookieSetStr));
    }
}
